<?php
/**
 * MachForm
 * 
 * © 2007–2025 Appnitro Software. All rights reserved.
 * 
 * This source code is proprietary and may not be copied, modified,
 * or distributed in any form without express written permission from Appnitro Software.
 * 
 * License information: https://www.machform.com/license-agreement/
 */

	require('config.php');
	require('lib/db-session-handler.php');
	require('includes/init.php');
	require('includes/db-core.php');
	require('includes/helper-functions.php');
	require('includes/check-session.php');
	
	require('includes/filter-functions.php');
	
	$_POST = json_decode(file_get_contents('php://input'), true);
	
	$widget_positions = $_POST['widget_positions'];
	$form_id		  = (int) $_POST['form_id'];


	$dbh = mf_connect_db();
	
	if(!empty($widget_positions) && is_array($widget_positions)){
		$widget_position = 1;
		foreach ($widget_positions as $chart_id) {
			$chart_id = (int) $chart_id;
			
			$query = "UPDATE ".MF_TABLE_PREFIX."report_elements SET chart_position = ? WHERE form_id = ? AND chart_id = ?";
			$params = array($widget_position,$form_id,$chart_id);
			mf_do_query($query,$params,$dbh);
			$widget_position++;
		}
	}
	
	$response_data = new stdClass();
	$response_data->status = "ok";
	
	$response_json = json_encode($response_data);
	
	header("Content-Type: application/json");
	echo $response_json;
?>